/**
 *  This is a class to allow for use of arrays with the speed of primitive
 *  arrays, but the growability of vectors.
 */
package us.gaaoc.framework.util.collections;

/**
 *  This is a class to allow for use of arrays with the speed of primitive
 *  arrays, but the growability of vectors.
 *  
 *  Reference:
 *  <ul>
 *    <li> http://www.javaworld.com/javaworld/jw-08-1999/jw-08-cooltools.html?page=1 </li>
 *  </ul>
 *  
 * @author mcelratr
 *
 */
public class FastGrowableFloatArray {
	    int sp = 0; // "stack pointer" to keep track of position in the array
	    private float[] array;
	    private int growthSize;
	    private static int defaultSize = 512;
	    // Add 1/4 of initial size when growing
	    private static int defaultGrowthSizeFactor = 4;  
	    
	    public FastGrowableFloatArray()
	    {
	        this(defaultSize);
	    }
	    
	    public FastGrowableFloatArray( int initialSize )
	    {
	        this( initialSize, (int)( initialSize / defaultGrowthSizeFactor ) );
	    }
	    
	    public FastGrowableFloatArray( int initialSize, int growthSize )
	    {
	        this.growthSize = growthSize;
	        array = new float[ initialSize ];
	    }
	    public void add( float f )
	    {
	        if( sp >= array.length ) // time to grow!
	        {
	            float[] tmpArray = new float[ array.length + growthSize ];
	            System.arraycopy( array, 0, tmpArray, 0, array.length );
	            array = tmpArray;
	        }
	        array[ sp ] = f;
	        sp += 1;
	    }
	    public float[] toArray()
	    {
	        float[] trimmedArray = new float[ sp ];
	        System.arraycopy( array, 0, trimmedArray, 0, trimmedArray.length );        
	        return trimmedArray;
	    }

}
